package qc.module.ehs.service;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.common.core.exception.QCPromptException;
import qc.common.core.unify.QCUnifyReturnValue;
import qc.module.ehs.entity.EhsHazardLateststatus;
import qc.module.ehs.repository.EhsHazardLateststatusRepository;

/**
 * 危险源最新状态信息Service
 *
 * @author QuCheng Tech
 * @create 2024/6/28
 */
@Service
public class EhsHazardLateststatusService {
    
    @Autowired
    private EhsHazardLateststatusRepository repository;

    /**
     * 新增危险源最新状态信息
     * 
     * @param entity
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/6/28
     */
    public String addHazardLatestStatus(EhsHazardLateststatus entity){
        if (entity == null) return QCUnifyReturnValue.Warn("新增对象不能为空");
        
        if (repository.insert(entity) < 0x1){
            return QCUnifyReturnValue.Warn("保存失败");
        }
        return QCUnifyReturnValue.Success();
    }

    /**
     * 修改危险源最新状态信息
     *
     * @param entity
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/7/4
     */
    public String updateHazardLatestStatus(EhsHazardLateststatus entity){
        if (entity == null) return QCUnifyReturnValue.Warn("对象不能为空");

        repository.updateById(entity);
        return QCUnifyReturnValue.Success();
    }

    /**
     * 删除指定危险源最新状态
     * 
     * @param hazardId 危险源ID
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/7/1
     */
    public String deleteHazardLatestStatus(String hazardId){
        repository.deleteById(hazardId);
        return QCUnifyReturnValue.Success();
    }

    /**
     * 获取指定危险源最新状态信息
     * 
     * @param hazardId 危险源ID
     * @return EhsHazardLateststatus
     * @throws QCPromptException
     * @author  QuCheng Tech
     * @since 2024/7/4
     */
    public EhsHazardLateststatus getByHazardId(String hazardId) throws QCPromptException {
        if (StringUtils.isBlank(hazardId)) throw new QCPromptException("危险源ID不能为空");
        
        return repository.selectById(hazardId);
    }
}
